package org.jetnuke.modules.news;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.dao.UserDAO;
import net.jforum.entities.User;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.StoriesDAO;
import org.jetnuke.entities.Comments;
import org.jetnuke.entities.Stories;

public class Story implements IAction {
	int id;

	public void setId(int id) {
		this.id = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		StoriesDAO md=DataAccessDriver.getInst().newStoriesDAO();
		Stories st=md.retrieveByPK(id);
		md.incCounter(id);
		HashMap userMap=new HashMap();
		vc.put("userMap",userMap);
		UserDAO um=DataAccessDriver.getInst().newUserDAO();
		User user=um.selectById(st.getAID());
		userMap.put(new Integer(st.getAID()),user);
		if(st.getINFORMANT()>=2){
			user=um.selectById(st.getINFORMANT());
			userMap.put(new Integer(st.getINFORMANT()),user);
		}
		
		//stories st = storiesPeer.retrieveByPK(id);
		//st.setCOUNTER(st.getCOUNTER() + 1);
		vc.put("topic", DataAccessDriver.getInst().newTopicsDAO().retrieveByPK(st.getTOPIC()));
		//md.update(st);
		vc.put("story", st);
		List level0 =DataAccessDriver.getInst().newCommentDAO().selectBySID_PID(id,0); 
			//commentsPeer.doSelect(new Criteria().add(
				//commentsPeer.SID, id).add(commentsPeer.PID, 0));
		Iterator it = level0.iterator();
		while (it.hasNext()) {
			Comments comm = (Comments) it.next();
			fillKids(comm,userMap);
		}
		vc.put("comments", level0);
		Stories topStory = DataAccessDriver.getInst().newStoriesDAO().getTopByTopic(st.getTOPIC());
			
			//storiesPeer.doSelect(new Criteria().add(storiesPeer.TOPIC,
			//	st.getTOPIC()).addDescendingOrderByColumn(storiesPeer.COUNTER)
			//	.setLimit(1));
		vc.put("topstory", topStory);
		
		int score=0;
		if(st.getRATINGS()>0)
			score=(int)(((double)st.getSCORE())/st.getRATINGS());
		vc.put("rate",new Integer(score));
		return new ActionForward(op, info);
	}

	private void fillKids(Comments comm, HashMap userMap) throws Exception {
		UserDAO um=DataAccessDriver.getInst().newUserDAO();
		List ls = 
			DataAccessDriver.getInst().newCommentDAO().selectBySID_PID(id,comm.getTID());
			//commentsPeer
			//	.doSelect(new Criteria().add(commentsPeer.SID, id).add(
				//		commentsPeer.PID, comm.getTID()));
		comm.setKids(ls);
		Iterator it = ls.iterator();
		while (it.hasNext()) {
			Comments comm_ = (Comments) it.next();
			int uid=comm_.getNAME();
			Integer itn=new Integer(uid);
			if(!userMap.containsKey(itn)){
				User user=um.selectById(uid);
				userMap.put(itn,user);
			}
			fillKids(comm_,  userMap);
		}
	}

}
